package com.dsg.controller.set;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyCus;
import com.dsg.entity.model.set.DsgSetCusBacklistModel;
import com.dsg.entity.set.DsgSetCusBacklist;
import com.dsg.service.apply.IDsgApplyCusService;
import com.dsg.service.set.IDsgSetCusBacklistService;
import com.dsg.support.LeveldbAccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 客戶/公司黑名單 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-08-18d
 */
@RestController
@RequestMapping("/set/dsgSetCusBacklist")
@Api(description = "客戶/公司黑名單")
public class DsgSetCusBacklistController extends BaseController {

    @Autowired
    private IDsgSetCusBacklistService service;
    @Autowired
    private IDsgApplyCusService dsgApplyCusService;
    @Autowired
    private LeveldbAccess leveldbAccess;

    /**
     * 查询列表（分页）
     *
     * @param dsgSetCusBacklist
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgSetCusBacklist dsgSetCusBacklist) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgSetCusBacklist.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgSetCusBacklist);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgSetCusBacklist
     * @return
     */
    @GetMapping(value = "/listCus")
    @ApiOperation("查询列表（分页）")
    public AjaxResult listCus(DsgSetCusBacklist dsgSetCusBacklist) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("createTime");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgSetCusBacklist.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByCusList(page, dsgSetCusBacklist);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增黑名單-客
     *
     * @param dsgSetCusBacklist
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增黑名單-客")
    public AjaxResult add(@RequestBody DsgSetCusBacklist dsgSetCusBacklist) {
        dsgSetCusBacklist.setOid(UUIDGenerator.getUUID());
        //查詢客戶資料
        QueryWrapper<DsgApplyCus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID_NO", dsgSetCusBacklist.getIdCard());
        DsgApplyCus dsgApplyCus = dsgApplyCusService.getOne(queryWrapper, false);
        if (dsgApplyCus != null) {
            dsgSetCusBacklist.setOwnsType(DsgSetCusBacklist.OWNS_TYPE_CUS);
            dsgSetCusBacklist.setCusId(dsgApplyCus.getOid());
            dsgSetCusBacklist.setCusNo(dsgApplyCus.getCusNo());
        }
        boolean flag = service.save(dsgSetCusBacklist);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @GetMapping(value = "/{id}/view")
    @ApiOperation("cs入机详细资料")
    public AjaxResult view(@PathVariable String id) {
        DsgSetCusBacklist dsgSetCusBacklist = service.getById(id);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgSetCusBacklist);
    }

    /**
     * 更新
     *
     * @param dsgSetCusBacklist
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgSetCusBacklist dsgSetCusBacklist) {
        AjaxResult ajaxResult;
        boolean flag = service.updateById(dsgSetCusBacklist);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgSetCusBacklist);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = service.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 上載
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation("上載")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        EasyExcel.read(file.getInputStream(), DsgSetCusBacklistModel.class, new UploadDsgSetCusBacklist(service, dsgApplyCusService, leveldbAccess)).sheet(0).headRowNumber(1).doRead();
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }
}
